/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 *
 * @author Michael
 */
class LeseThreat implements Runnable {
static int length = 256;
DatagramSocket socket;
Vector clients;
static int x,y,spielerAnz;
static int[] positionen;


LeseThreat(DatagramSocket socket, Vector clients) {
    this.socket = socket;
    this.clients = clients;
    this.positionen = new int[2*clients.size()];        
    Thread t = new Thread(this);
    t.start();
}

public void run() {
    System.out.println("Neuer Threat erstellt: Lesen");
    DatagramPacket packet = new DatagramPacket( new byte[length], length);
    while( true ) {
        try {
            socket.receive( packet );
            InetSocketAddress add = (InetSocketAddress)packet.getSocketAddress();
            for(int i=0;i<clients.size();i++){
                InetSocketAddress dest = (InetSocketAddress) clients.get(i);
                if(dest.equals(add)){
                    String text = new String(packet.getData(), 0, packet.getLength());
                    System.out.println("Threat " + add + " (Spieler " +i + "):" + text);
                    int Beginn=0;
                    int Ende;
                    for(int l=0;l<text.length();l++){
                        if(text.charAt(l)==';'){
                            Ende=l;                    
                            try {
                                positionen[i*2] = Integer.parseInt((text.substring(Beginn,Ende)));
                                System.out.println("Threat "+ add + " (Spieler " +i + "):" + positionen[i*2]);
                            } catch (Exception E){
                                //es war keine Zahl
                            }                        
                            Beginn=l+1;
                            try {
                                positionen[i*2+1] = Integer.parseInt((text.substring(Beginn,text.length())));
                                System.out.println("Threat "+ add + " (Spieler " +i + "):" + positionen[i*2+1]);
                            } catch (Exception E){
                                //es war keine Zahl
                            }
                        }
                    }                
                }
            }
        }
        catch( IOException e ) {
            System.err.println("Ausnahmefehler: " + e );
        }
    }
}
public static int[] getPos(){
    return positionen;
}
}

